curr_dir:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))
###################
### BLACKPARROT ###
###################
BLACKPARROT = ${root_dir}/../third_party/black_parrot
BLACKPARROT_PATCHES_DIR = ${curr_dir}/black_parrot_patches/
BLACKPARROT_CFG ?= e_bp_unicore_cfg
BLACKPARROT_UHDM = ${root_dir}/build/surelog/bp_tethered.${BLACKPARROT_CFG}.none.parse/out/slpp_unit/surelog.uhdm
CONFIG_FILE = ${curr_dir}/config.mk
ORFS_DIR = ${root_dir}/../third_party/OpenROAD-flow-scripts
ORFS_PATCHES_DIR = ${curr_dir}/orfs_patches/

export BP_TOOLS_DIR = $(BLACKPARROT)/../black_parrot_tools
export BP_SDK_DIR = $(BLACKPARROT)/../black_parrot_sdk

${ORFS_DIR}/.gitpatch:
	cd $(ORFS_DIR) && git apply ${ORFS_PATCHES_DIR}/*.patch && touch $@

${BLACKPARROT}/.gitpatch:
	cd ${BLACKPARROT}/ && git apply ${BLACKPARROT_PATCHES_DIR}/*.patch && touch $@

${BLACKPARROT_UHDM}: clean-build | ${BLACKPARROT}/.gitpatch
	(export PATH=${OUT_DIR}/bin:${PATH} && \
		cd ${BLACKPARROT}/bp_top/syn/ && SURELOG_OPTS="-synth -DSYNTHESIS=1" $(MAKE) parse.surelog CFG=${BLACKPARROT_CFG} RESULTS_PATH=${root_dir}/build)

uhdm/synlig/synth-blackparrot-build: | ${BLACKPARROT_UHDM}
	(export PATH=${OUT_DIR}/bin:${PATH} && \
		cd ${root_dir}/build && \
		synlig \
			-p "read_uhdm ${BLACKPARROT_UHDM}" \
			-p "synth_xilinx -iopad -family xc7" \
			-p "write_edif bp_${BLACKPARROT_CFG}.edif")

uhdm/synlig/synth-blackparrot-build-python: | ${BLACKPARROT_UHDM}
	(cd ${root_dir}/build && \
		${root_dir}/black_parrot/black_parrot_build.py \
			--mod_dir ${OUT_DIR} \
			--uhdm_file ${BLACKPARROT_UHDM} \
			--edif_file bp_${BLACKPARROT_CFG}.edif)

# Exports required for ASIC synthesis flow
export BLACKPARROT_BUILD_DIR=${root_dir}/build/surelog/bp_tethered.${BLACKPARROT_CFG}.none.parse
export YOSYS_CMD=${OUT_DIR}/bin/synlig
export BLACKPARROT_UHDM
export TEST_DIR
uhdm/synlig/synth-blackparrot-build-asic: ${ORFS_DIR}/.gitpatch | ${BLACKPARROT_UHDM}
	TOP=$(BLACKPARROT) $(MAKE) -C $(ORFS_DIR)/flow DESIGN_CONFIG=$(CONFIG_FILE) synth
